//
//  CCSettingsViewController.m
//  BuDeJie
//
//  Created by demoncraz on 2017/2/10.
//  Copyright © 2017年 demoncraz. All rights reserved.
//

#import "CCSettingsViewController.h"
#import <SDWebImage/SDImageCache.h>
#import "CCFileTool.h"

#define CachesPath [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]

static NSString * const cellId = @"cell";

@interface CCSettingsViewController ()

@property (nonatomic, assign) NSInteger totalSize;

@end

@implementation CCSettingsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellId];
    
    [self getCachesFileSize];
    
    [CCFileTool getFileSize:CachesPath completion:^(NSInteger totalSize) {
        _totalSize = totalSize;
        [self.tableView reloadData];
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)getCachesFileSize {
    
}


- (NSString *)fileSizeString {
    NSString *sizeStr = @"清除缓存";
    
    if (_totalSize >= 1000 * 1000) {//MB
        sizeStr = [NSString stringWithFormat:@"%@（%.1fMB）", sizeStr, _totalSize / 1000.0 / 1000.0];
    } else if (_totalSize >= 1000) {//KB
        sizeStr = [NSString stringWithFormat:@"%@（%.1fKB）", sizeStr, _totalSize / 1000.0];
    } else if (_totalSize > 0) {//B
        sizeStr = [NSString stringWithFormat:@"%@（%.ldB）", sizeStr, _totalSize];
    }
    return sizeStr;
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId forIndexPath:indexPath];
    
    
    cell.textLabel.text = [self fileSizeString];
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //清除缓存
    [CCFileTool deleteFilesAtDirectoryPath:CachesPath];
    
    self.totalSize = 0;
    
    [self.tableView reloadData];
}


@end
